package com.stock.publiclib.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * ---------------------------------------------------
 * Description: 数据库关联类
 * Author: ljgsonx
 * Belong to: com.example.administrator.alldemos
 * Time: 2015/11/19 10:05
 * ---------------------------------------------------
 */
public class CommonDBOpenHelper extends SQLiteOpenHelper {

    private final static String TAG = "CommonDBOpenHelper--->";
    private int version;
    private String commondb_name;

    public CommonDBOpenHelper(Context context,String commondb_name, int version) {
        super(context, commondb_name, null, version);
        this.commondb_name = commondb_name;
        this.version = version;
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i(TAG, "onUpgrage");
//        db.execSQL("DROP TABLE IF EXISTS USER");
//        onCreate(db);
//        //可以支持版本回滚
//        if(oldVersion == 1 && newVersion == 2){
//            //create a new table...
//        }else if(newVersion == 2 && oldVersion == 1){
//            //drop the new table...
//        }
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.i(TAG, "onCreate");
        db.execSQL("CREATE TABLE IF NOT EXISTS " + getCommonDBName() + "(key VARCHAR(100),value VARCHAR(100))");
    }

    public String getCommonDBName(){
        return commondb_name.split("\\.")[0];
    }

}
